package org.example.filter;

import org.example.dao.UriViewDao;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * 访问统计过滤器
 */
public class AccessCountFilter implements Filter {
    
    private UriViewDao uriViewDao;
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        uriViewDao = new UriViewDao();
    }
    
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String uri = httpRequest.getRequestURI();
        
        // 记录访问统计
        uriViewDao.incrementViewCount(uri);
        
        // 继续处理请求
        chain.doFilter(request, response);
    }
    
    @Override
    public void destroy() {
        // 清理资源
    }
}
